package stef;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;


public class MainScreen extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;

	private USBFileTransfer chooseGPS;
	private JRadioButton garmin,mio;
	private FileSearchButton fileSearchBtn;
	private GPSSearchButton gpsSearchBtn;
	private FileScrollPanel fileScrollPanel;
	private TransferButton transferButton;

	private String path;
	public MainScreen(){
		super("GPS file transfer");
		setBounds(350,50,300, 600);
		setResizable(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


		setLayout(new BorderLayout());
		
		ButtonGroup group = new ButtonGroup();
		garmin = new JRadioButton("Garmin");
		mio = new JRadioButton("Mio");
		group.add(garmin);
		group.add(mio);
		
		Container c = new Container();
		c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
		//c.setPreferredSize(new Dimension(300, 80));
		c.add(garmin);
		c.add(mio);
		
		fileSearchBtn = new FileSearchButton();
		c.add(fileSearchBtn);
		add(c, BorderLayout.NORTH);
		
		
		fileScrollPanel = new FileScrollPanel();
		add(fileScrollPanel, BorderLayout.CENTER);

		Container bottom = new Container();
		bottom.setLayout(new BoxLayout(bottom, BoxLayout.Y_AXIS));
		gpsSearchBtn = new GPSSearchButton();
		bottom.add(gpsSearchBtn);

		transferButton = new TransferButton();
		bottom.add(transferButton);
		
		add(bottom, BorderLayout.SOUTH);
		
		fileSearchBtn.addActionListener(this);
		gpsSearchBtn.addActionListener(this);
		transferButton.addActionListener(this);
		

		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource().equals(fileSearchBtn)){
			JFileChooser chooser = new JFileChooser();
			chooser.setMultiSelectionEnabled(true);
			int value = chooser.showOpenDialog(null);
			if(value==JFileChooser.APPROVE_OPTION){
				File[] files =chooser.getSelectedFiles();
				fileScrollPanel.addData(getGPSFiles(files));
			}
		}else if (e.getSource().equals(gpsSearchBtn)){
				chooseGPS = new USBFileTransfer();
				chooseGPS.chooseGPS();
		}else if (e.getSource().equals(transferButton)){
			if(chooseGPS==null || chooseGPS.getGPS()==null){
				JOptionPane.showMessageDialog(null, "You have to select a device");
				return;
			}
			if(garmin.isSelected()){
				path = "Garmin\\GPX\\";
			}else if(mio.isSelected()){
				path = "Dodge\\tracks\\";
			}
			else {
				JOptionPane.showMessageDialog(null, "You have to select a type of device, Garmin or Mio");
				return;
			}
			
			chooseGPS.transferFiles(fileScrollPanel.getFiles(), path);
		}

	}

	private List<GPSFile> getGPSFiles(File[] files) {
		List<GPSFile> fileList = new ArrayList<>();

		List<File> errors = new ArrayList<>();
		for(File file: files){
			if(isGPXFile(file)){
				fileList.add(new GPSFile(file));
			}else errors.add(file);
		}

		String message = "You can only select .gpx files, the folowing files don't match that:\n";
		for(File file: errors){
			message += file.getName() +"\n";
		}
		if(!errors.isEmpty())
			JOptionPane.showMessageDialog(null, message);
		return fileList;
	}

	private boolean isGPXFile(File file) {
		int extensionIndex = file.getPath().lastIndexOf(".");
		String extension = file.getPath().substring(extensionIndex);

		return extension.equals(".gpx");
	}
}
